@model List<CaipuGuanliXitong.Models.RecipeIngredient>

@{
    ViewData["Title"] = "带量食材管理";
}

<h2>带量食材管理</h2>

@if (ViewBag.Message != null)
{
    <div class="alert @(ViewBag.IsSuccess ? "alert-success" : "alert-danger") alert-dismissible fade show" role="alert">
        @ViewBag.Message
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
}

<div class="row mb-3">
    <div class="col-md-6">
        <a asp-action="Create" class="btn btn-primary">
            <i class="fas fa-plus-circle"></i> 新增带量食材
        </a>
        <a asp-action="CreateMultiple" class="btn btn-info">
            <i class="fas fa-plus-square"></i> 批量新增带量食材
        </a>
        <a asp-action="ExportToExcel" class="btn btn-success">
            <i class="fas fa-file-export"></i> 导出Excel
        </a>
    </div>
    <div class="col-md-6">
        <form asp-action="ImportFromExcel" method="post" enctype="multipart/form-data" class="form-inline justify-content-end">
            <div class="form-group">
                <input type="file" name="file" class="form-control-file" accept=".xlsx,.xls" />
            </div>
            <button type="submit" class="btn btn-info ml-2">
                <i class="fas fa-file-import"></i> 导入Excel
            </button>
        </form>
    </div>
</div>

<form asp-action="Index" method="get">
    <div class="form-actions no-color">
        <p>
            搜索: <input type="text" name="SearchString" value="@ViewBag.CurrentFilter" />
            <input type="submit" value="搜索" class="btn btn-default" /> |
            <a asp-action="Index">返回完整列表</a>
        </p>
    </div>
</form>

<table class="table">
    <thead>
        <tr>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.CookBookNameSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">菜谱名称</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.IngredientNameSortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">食材名称</a>
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewBag.QuantitySortParm" asp-route-currentFilter="@ViewBag.CurrentFilter">用量</a>
            </th>
            <th>
                单价
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CookBookName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IngredientName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quantity)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-info btn-sm">编辑</a>
                    <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-success btn-sm">详情</a>
                    <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger btn-sm">删除</a>
                </td>
            </tr>
        }
    </tbody>
</table>

<div>
    @{
        var totalItems = (int)ViewBag.TotalItems;
        var currentPage = (int)ViewBag.CurrentPage;
        var pageSize = (int)ViewBag.PageSize;
        var totalPages = (int)Math.Ceiling((double)totalItems / pageSize);

        if (totalPages > 1)
        {
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    @if (currentPage > 1)
                    {
                        <li>
                            <a asp-action="Index" asp-route-page="@(currentPage - 1)" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    }

                    @for (var i = 1; i <= totalPages; i++)
                    {
                        <li class="@(i == currentPage ? "active" : "")">
                            <a asp-action="Index" asp-route-page="@i" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort">@i</a>
                        </li>
                    }

                    @if (currentPage < totalPages)
                    {
                        <li>
                            <a asp-action="Index" asp-route-page="@(currentPage + 1)" asp-route-currentFilter="@ViewBag.CurrentFilter" asp-route-sortOrder="@ViewBag.CurrentSort" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    }
                </ul>
            </nav>
        }
    }
</div>